﻿#include "UserInformation.h"
#include <qmessagebox.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <vector>
#include <Constants.h>
#include <Global_Class.h>
#include <regex>
#include <AllData.h>
using namespace Constants;


UserInformation::UserInformation(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	ui.UsernameLine->setEnabled(false);
	connect(ui.ModifyBtn, &QPushButton::clicked, this, [=] {
		std::regex phoneRegex("^1[3-9]\\d{9}$");
		if (!std::regex_match(ui.PhoneLine->text().toStdString(), phoneRegex)) {
			QMessageBox::warning(this, "修改失败", "手机号格式不正确！");
			return;
		}
		
		std::regex idCardRegex("^\\d{17}[\\dXx]$");
		if (!std::regex_match(ui.IdCardLine->text().toStdString(), idCardRegex)) {
			QMessageBox::warning(this, "修改失败", "身份证号格式不正确！");
			return;
		}
		//TODO 实现用户信息修改
		userWindow->setCurrentUser(users(
			currentUser.getUserId(), // 保持用户ID不变
			currentUser.getUsername(), // 保持用户名不变
			ui.DisplayNameLine->text().toStdString(),
			currentUser.getPassword(), // 保持密码不变
			ui.PhoneLine->text().toStdString(),
			ui.RoleLine->text().toStdString(),
			ui.IdCardLine->text().toStdString()
		));
		for (auto it = allData->usersQueue.begin(); it != allData->usersQueue.end(); it++) {
			if (it->getUserId() == currentUser.getUserId()) {
				it->setDisplayName(ui.DisplayNameLine->text().toStdString());
				it->setPhone(ui.PhoneLine->text().toStdString());
				it->setIdCard(ui.IdCardLine->text().toStdString());
				// 更新用户信息
			}
		}
		QMessageBox::information(this, "Modify", "用户信息已被修改!");
		allData->User_WriteToFile(USER_INFORMATION_FILE_POSITION);
		});
	connect(ui.QuitBtn, &QPushButton::clicked, this, [=] {
		QMessageBox::information(this, "Cancel", "关闭当前页面!");
		this->close();
		});
}

UserInformation::~UserInformation()
{ 

}

void UserInformation::SetupUserInformation(users user)
{
	//TODO 测试样例 待完善
	// 设置用户信息到界面
	setCurrentUser(user); // 设置当前用户信息
	ui.UsernameLine->setText(QString::fromStdString(user.getUsername()));
	ui.DisplayNameLine->setText(QString::fromStdString(user.getDisplayName()));
	ui.PhoneLine->setText(QString::fromStdString(user.getPhone()));
	ui.RoleLine->setText(QString::fromStdString(user.getRole()));
	ui.IdCardLine->setText(QString::fromStdString(user.getIdCard()));
	ui.RoleLine->setReadOnly(true); // 身份不可修改


		
}

